package com.dcivision.dms.core;


import java.io.File;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;

// Referenced classes of package com.dci.dms.util.client:
// PropertyGetter, HttpSender, HtmlFormText, HtmlFormFile

public class DmsEmailArchiveClearCacheFile{

  public static final String REVISION = "$Revision: 1.2 $";
  
  private static Object lock = new Object();
  private String timeInterval  = "";

  
  protected Log log = LogFactory.getLog(this.getClass().getName());  
  
  public DmsEmailArchiveClearCacheFile(){
	  
  }
  

  public void clearCacheFile(){
    // delete file when the file is old than current time 7 days
//	Date date = new Date();
    long currentDateTime = new Date().getTime();	  	  
    String folderName = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DMS_EMAILARCHIVE_CACHEFOLDER_PATH);
//    String folderName = "D:\\ParaDM\\file\\ParaDOC\\cache";
    File catcheFolder = new File(folderName);
    File[] listFile;
    if (catcheFolder.exists()) {
   	  listFile = catcheFolder.listFiles();
   	  if(!Utility.isEmpty(listFile)){
   	    for(int i = 0 ; i< listFile.length ;i++){
    	  File file  = listFile[i];
   	      String fileName = file.getName();
      	  String[] fileNameArr = TextUtility.splitString(fileName,"_");
   	      String fileNameTimeStr = fileNameArr[1];
   	   	  long fileNameTime = TextUtility.parseLong(fileNameTimeStr);
   		  Timestamp tdate = new Timestamp(fileNameTime);
   		  tdate = Utility.addDay(tdate,7);   	   	  
   	      //delete cache file
   	      if(currentDateTime > tdate.getTime()){
   	    	file.delete();
   	      }
   	      file = null;
   	    }   		  
   	  }   	  
    }  
  }

  public static void main(String args[]) throws Exception  {
	  DmsEmailArchiveClearCacheFile cat = new DmsEmailArchiveClearCacheFile();
	  cat.clearCacheFile();
  }


}